﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Rj_GsInsurance.Server.Models.InsurMsgDataInput;
using Rj_GsInsurance.Server.Models.InsurMsgDataOutput;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wpf.Ui.Controls;
using Wpf.Ui;
using Rj_GsInsurance.Server.Extensions;
using Rj_GsInsurance.Server.DataBaseContext;
using Microsoft.EntityFrameworkCore;
using Rj_GsInsurance.Server.DataBaseContext.Entities;

namespace Rj_GsInsurance.Client.ViewModels
{
    public partial class DevUploadViewModel: ObservableObject
    {
        public ISnackbarService SnackbarService { get; set; }
        public HisDbContext HisDbContext { get; set; }
        [ObservableProperty]
        private string _SearchDeptCode;
        [ObservableProperty]
        private string _SearchHisDeptCode;
        [ObservableProperty]
        private string _SearchHisDeptName;        
        [ObservableProperty]
        private List<QueryDeptOutput> _DeptList;
        [ObservableProperty]
        private QueryDeptOutput _SelectedDept;        
        [ObservableProperty]
        private List<SystemTbDepartment> _HisDeptList;
        [ObservableProperty]
        private SystemTbDepartment _SelectedHisDept;
        [ObservableProperty]
        private string _Zgs;
        [ObservableProperty]
        private string _Cws;
        [ObservableProperty]
        private string _Fzr;
        [ObservableProperty]
        private string _Fzrdh;

        public DevUploadViewModel(ISnackbarService snackbarService, HisDbContext hisDbContext)
        {
            SnackbarService = snackbarService;
            HisDbContext = hisDbContext;
        }

        [RelayCommand]
        private void OnQueryDept(object param)
        {
            QueryDeptInput input = new QueryDeptInput()
            {
                DeptCode = SearchDeptCode,
            };
            try
            {
                var insurMsgOut = input.Excute<QueryDeptInput, List<QueryDeptOutput>>("1105");
                DeptList = insurMsgOut.output;
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                    "警告!",
                    $"医保交易失败！{ex.Message}",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
            }
        }

        [RelayCommand]
        private void OnCancelUpload(object param)
        {
            if (SelectedDept == null || string.IsNullOrWhiteSpace(SelectedDept.Code))
            {
                SnackbarService.Show(
                    "警告!",
                    $"请选择需要取消的科室",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
                return;
            }
            DeptCancelUploadInput input = new DeptCancelUploadInput()
            {
                DeptCode = SelectedDept.Code,
                DeptName = SelectedDept.Name,
            };
            try
            {
                var insurMsgOut = input.Excute<DeptCancelUploadInput, DeptCancelUploadOutput>("3501");
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                    "警告!",
                    $"医保交易失败！{ex.Message}",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
            }

            SnackbarService.Show(
                    "警告!",
                    $"撤销成功!",
                    ControlAppearance.Success,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
        }

        [RelayCommand]
        private void OnUpload(object param)
        {
            if (SelectedHisDept == null)
            {
                SnackbarService.Show(
                    "警告!",
                    $"请选择需要上传的科室",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
                return;
            }
            DeptUploadInput input = new DeptUploadInput()
            {
                Code = SelectedHisDept.DeptCode,
                Name = SelectedHisDept.DeptName,
                BedNum=Cws,
                WorkerNum=Zgs,
                ChargePerson=Fzr,
                ChargePersonTel=Fzrdh,
                Jbr="管理员",
                Jbsj=DateTime.Now.ToString("yyyyMMddHHmmss")
            };
            try
            {
                var insurMsgOut = input.Excute<DeptUploadInput, DeptUploadOutput>("3500");
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                    "警告!",
                    $"医保交易失败！{ex.Message}",
                    ControlAppearance.Danger,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
            }

            SnackbarService.Show(
                    "提示!",
                    $"上传成功!",
                    ControlAppearance.Success,
                    new SymbolIcon(SymbolRegular.Fluent24),
                    TimeSpan.FromSeconds(30)
                );
        }

        [RelayCommand]
        private async void OnQueryHisDept(object param)
        {
            HisDeptList= await HisDbContext.SystemTbDepartments.Where(w => (w.DeptCode == SearchHisDeptCode) ||( w.DeptName.Contains(SearchHisDeptName))).ToListAsync();
        }
    }
}
